package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.fastdfs.FastDFSClientUtil;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.threadlocal.WmThreadLocalUtils;
import com.usian.wemedia.mapper.WmMaterialMapper;
import com.usian.wemedia.mapper.WmNewsMapper;
import com.usian.wemedia.mapper.WmNewsMaterialMapper;
import com.usian.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    FastDFSClientUtil fastDFSClientUtil;

    @Value("${fileServerUrl}")
    private String a;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    @Autowired
    private WmNewsMapper wmNewsMapper;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        try {
            //检查参数
            if(multipartFile==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            //上传图片到fasfdfs
            String path = fastDFSClientUtil.uploadFile(multipartFile);

            WmUser user = WmThreadLocalUtils.getUser();

            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUrl(path);
            wmMaterial.setIsCollection((short)0);
            wmMaterial.setUserId(user.getId());
            wmMaterial.setType((short)0);
            wmMaterial.setCreatedTime(new Date());
            save(wmMaterial);


        }catch (Exception e){
            e.printStackTrace();
        }


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //检查参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //带条件分页查询
        LambdaQueryWrapper<WmMaterial>  lambdaQueryWrapper =new LambdaQueryWrapper<>();
        //从本地线程获取当前用户//根据用户ID查询素材表
        WmUser user = WmThreadLocalUtils.getUser();
        if (user!=null){
            lambdaQueryWrapper.eq(WmMaterial::getUserId,user.getId());
        }

        if (dto.getIsCollection()!=null&& dto.getIsCollection()==1){
            lambdaQueryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        Page page = new Page(dto.getPage(), dto.getSize());
        IPage result=page(page,lambdaQueryWrapper);
        ResponseResult responseResult=new PageResponseResult(dto.getPage(),dto.getSize(),(int)result.getTotal());
        List<WmMaterial> list = result.getRecords();
        for (WmMaterial material : list) {
            material.setUrl(a+material.getUrl());
        }
        responseResult.setData(list);


        return responseResult;
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmMaterial material = getById(id);
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //根据素材id查询是否被引用
        QueryWrapper<WmNewsMaterial> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(WmNewsMaterial::getMaterialId,id);

        Integer count = wmNewsMaterialMapper.selectCount(queryWrapper);
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }

        //删除tupa
        fastDFSClientUtil.delFile(material.getUrl());
        //删除素材表信息
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
